package com.zb.stu.controller;

import com.alibaba.fastjson.JSONObject;
import com.zb.stu.entity.Category;
import com.zb.stu.result.Result;
import com.zb.stu.result.Text;
import com.zb.stu.service.CategoryService;
import com.zb.stu.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.Future;

/**
 * Created by user on 2018/11/27.
 */
@Controller
public class StandardController {
    @Autowired
    private StandardService service;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private StringRedisTemplate redis;

    @RequestMapping("/")
    public String showStandard(String name, Model model){
        Result result=new Result();
        result.setTarget(service.listStandard(name));
        model.addAttribute("r",result);
        return "index";
    }

    @RequestMapping("/toadd")
    public String toadd(){
        return "info";
    }

    @RequestMapping("/info")
    @ResponseBody
    public String shownfo(){
        String clist=redis.opsForValue().get("clist");
        Text ta=new Text();
        Long start=System.currentTimeMillis();
        Future<String> a= ta.task1();
        Future<String> a1=ta.task2();
        Future<String> a2=ta.task3();
        for (;;){
            if (a.isDone() && a1.isDone() && a2.isDone()){
                break;
            }
        }
        Long end=System.currentTimeMillis();
        System.out.println(end-start);
        if (StringUtils.isEmpty(clist)){
            List<Category> list = categoryService.listCategory();
            clist= JSONObject.toJSONString(list);
            redis.opsForValue().set("clist",clist);
        }
        return clist;
    }
}
